package com.jstyle.beatecg.activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.jstyle.beatecg.BuildConfig;
import com.jstyle.beatecg.R;
import com.jstyle.beatecg.base.BaseActivity;
import com.jstyle.beatecg.ble.BleManager;
import com.jstyle.beatecg.ble.BleService;
import com.jstyle.beatecg.model.BleData;
import com.jstyle.beatecg.util.ResolveData;
import com.jstyle.beatecg.util.RxBus;
import com.jstyle.beatecg.util.ScreenUtils;
import com.jstyle.beatecg.util.SendData;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 关于我们
 */

public class AboutUsActivity extends BaseActivity {

    @BindView(R.id.ll_title)
    LinearLayout llTitle;

    @BindView(R.id.bt_app_version)
    Button btAppVersion;
    @BindView(R.id.bt_device_version)
    Button btDeviceVersion;
    @BindView(R.id.bt_back)
    ImageView btBack;
    @BindView(R.id.button_toFeedback)
    Button button2;
    @BindView(R.id.bt_toWebsite)
    Button bToWebsite;
    @BindView(R.id.imageView_logo)
    ImageView imageViewLogo;
    private Disposable subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            showStatusBar();
        }
        PackageInfo packageInfo;
        try {
            packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            String versionName = packageInfo.versionName;
            btAppVersion.setText(versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        subscription = RxBus.getInstance().toObservable(BleData.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<BleData>() {
            @Override
            public void accept(BleData data) throws Exception {
                String action = data.getAction();
                if (BleService.ACTION_DATA_AVAILABLE.equals(action)) {
                    byte[] value = data.getValue();
                    if (value[0] == SendData.CMD_GET_VERSION) {
                        String version = ResolveData.getVersion(value[3] );
                        // String version = "055";
                        btDeviceVersion.setText(version);
                    }
                }
            }


        });


        //checkVersion();
    }

    @Override
    protected void showStatusBar() {
        ScreenUtils.setStatusBarColor(llTitle, Color.TRANSPARENT);
    }

    @OnClick({R.id.bt_back, R.id.button_toFeedback, R.id.bt_toWebsite})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.button_toFeedback:
                startActivity(FeedBackActivity.class);
                break;
            case R.id.bt_toWebsite:
              /*  Uri uri = Uri.parse("http://www.jointcorp.com/");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);*/
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unsubscribe();
    }

    private void unsubscribe() {
        if (subscription != null && !subscription.isDisposed()) {
            subscription.dispose();
        }
    }

    private void checkVersion() {
        if (BleManager.getInstance().isConnected()) {
            showProgressDialog(getString(R.string.check_firmware_version));
            BleManager.getInstance().writeValue(SendData.getDeviceVersion());
        }

    }
}
